var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    gender: ['男', '女'],
    showpicker: '',
    index: '',
    userinfo: {},
    placeholder: '',
    userimg:'',
    isselected:0,
    codeText:"发送验证码"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getxx();
    
  },

  // 获取默认信息
  getxx:function(){
    var userid = wx.getStorageSync('userId');
    var that = this;
    var getmsg ={
      url:'api/user/getUserInfo',
      data:{
        user_id:userid
      },
      success:function(e){
        console.log(e)
        var index;
        if (e.data.data.sex){
          index = parseInt(e.data.data.sex) - 1
        }else{
          that.setData({
            placeholder:'请选择'
          })
        }
        that.setData({
          userinfo:e.data.data,
          index:index
        })
      }
    };
    app.post(getmsg);
  },
  // 改变手机号
  changeMobile(e){
    let mobile = e.detail.value;
    console.log(mobile)
    let userinfo = this.data.userinfo;
    userinfo.mobile = mobile;
    this.setData({
      userinfo:userinfo
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  sendCode(){
    let mobile = this.data.userinfo.mobile;
    if(!(/^1[3456789]\d{9}$/.test(mobile))){  
      wx.showToast({
        title: '手机号码有误，请重填',
        icon:"none"
      })
      return false; 
  } 
      app.post({
        url:"api/user/sendSms",
        data:{
          mobile:mobile,
          user_id:wx.getStorageSync('userId')
        },
        success:res=>{
          console.log(res)
          if(res.data.code == 1){
            wx.showToast({
              title: '发送成功',
              icon:'none'
            })
            this.setData({
              codeText:"已发送"
            })
          }else{
            wx.showToast({
              title: res.data.msg,
              icon:'none'
            })
          }
        }
      })
  },
  // 添加图片
  chooseimg: function () {
    var that = this;
    var success=function(e){
      console.log(e)
      var data = JSON.parse(e.data);
      that.setData({
        userimg: data.data.url,
        isselected:1
      })
    };
    app.uploadimg(success);
  },

  // 选择性别
  sGender: function () {
    this.setData({
      showpicker: 1,
      index: 0
    })
  },
  // 隐藏picker
  hidePicker: function () {
    this.setData({
      showpicker: 0
    })
  },
  // 
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  // 提交信息
  userxx: function (e) {
    var userid = wx.getStorageSync('userId');
    var nickname = e.detail.value.nickname;
    var username = e.detail.value.username;
    var mobile = e.detail.value.mobile;
    var mobile_code = e.detail.value.mobile_code;
    if(!mobile_code){
      wx.showToast({
        title: '请获取验证码',
        icon:'none'
      })
      return
    }
    // 添加信息
    var that = this;
    var userxx={
      user_id:userid,
      sex:parseInt(that.data.index) + 1,
      username:username,
      nickname:nickname,
      mobile:mobile,
      mobile_code:mobile_code
    };
    if(that.data.userimg != ''){
      userxx.head = that.data.userimg;
    }
    var msg ={
      url:'api/userr/saveInfo',
      data:userxx,
      success:function(e){
        console.log(e)
          wx.showToast({
            title: e.data.msg,
            icon:"none",
            duration:2000
          });
          if(e.data.code == 1){
             wx.setStorageSync('status', 2);
              setTimeout(function(){
                  wx.switchTab({
                    url: '/pages/mine/user/user',
                  })
              },2000)
          }
      },
      fail:function(e){
        wx.showToast({
          title: e.data.msg,
          icon: "none",
          duration: 2000
        })
      }
    };
    app.post(msg);

  },
 

})